package dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import dto.RaspunsuriCompletateDTO;
import entities.RaspunsuriCompletate;

@Stateless
public class RaspunsuriCompletateDAO implements RaspunsuriCompletateDAORemote {

	@PersistenceContext
	EntityManager em;

	Conversion conv = new Conversion();

	@Override
	public void add(RaspunsuriCompletateDTO rcDTO) {
		try {
			RaspunsuriCompletate rc = new RaspunsuriCompletate();
			rc = conv.convertRaspunsuriCompletateDTO(rcDTO);
			em.persist(rc);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<RaspunsuriCompletateDTO> getRCByChestionarId(int cId) {
		List<RaspunsuriCompletateDTO> rcDTO = new ArrayList<RaspunsuriCompletateDTO>();
		List<RaspunsuriCompletate> rc = new ArrayList<RaspunsuriCompletate>();
		Query query = em.createNamedQuery("findRaspunsuriCompletateByChestionar");
		query.setParameter("Param", cId);
		rc = query.getResultList();
		for (int i = 0; i < rc.size(); i++){
			RaspunsuriCompletateDTO rcdto = new RaspunsuriCompletateDTO();
			rcdto = conv.convertRaspunsuriCompletateToDTO(rc.get(i));
			rcDTO.add(rcdto);
		}
		return rcDTO;
	}

	@Override
	public List<RaspunsuriCompletateDTO> getRCByIntrebareId(int id) {
		List<RaspunsuriCompletateDTO> rcDTO = new ArrayList<RaspunsuriCompletateDTO>();
		List<RaspunsuriCompletate> rc = new ArrayList<RaspunsuriCompletate>();
		Query query = em.createNamedQuery("findRaspunsuriCompletateByIntrebare");
		query.setParameter("Param", id);
		rc = query.getResultList();
		for (int i = 0; i < rc.size(); i++){
			RaspunsuriCompletateDTO rcdto = new RaspunsuriCompletateDTO();
			rcdto = conv.convertRaspunsuriCompletateToDTO(rc.get(i));
			rcDTO.add(rcdto);
		}
		return rcDTO;
	}

}
